<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head th:fragment="head(title)">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title th:text="${title} + ' - 图书馆管理系统'">图书馆管理系统</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 自定义CSS -->
    <link rel="stylesheet" th:href="@{/css/style.css}">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body>
    <!-- 导航栏 -->
    <nav th:fragment="navbar" class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" th:href="@{/}">
                <i class="fas fa-book-open me-2"></i>图书馆管理系统
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" th:href="@{/}">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" th:href="@{/books/list}">图书列表</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" th:href="@{/books/search}">搜索图书</a>
                    </li>
                    <!-- 添加熔断器测试链接 -->
                    <li class="nav-item">
                        <a class="nav-link" th:href="@{/books/circuit-breaker}">熔断器测试</a>
                    </li>
                    <!-- 添加Zipkin链路跟踪测试链接 -->
                    <li class="nav-item">
                        <a class="nav-link" th:href="@{/zipkin-test}">链路跟踪</a>
                    </li>
                    <!-- 已登录用户可见（非管理员） -->
                    <li class="nav-item" sec:authorize="isAuthenticated() and !hasRole('ADMIN')">
                        <a class="nav-link" th:href="@{/loans/my}">我的借阅</a>
                    </li>
                    <!-- 管理员可见 -->
                    <li class="nav-item dropdown" sec:authorize="hasRole('ADMIN')">
                        <a class="nav-link dropdown-toggle" href="#" id="adminDropdown" role="button" data-bs-toggle="dropdown">
                            管理功能
                        </a>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" th:href="@{/books/admin/add}">添加图书</a></li>
                            <li><a class="dropdown-item" th:href="@{/admin/users}">用户管理</a></li>
                            <li><a class="dropdown-item" th:href="@{/loans/admin/all-loans}">借阅管理</a></li>
                            <li><a class="dropdown-item" th:href="@{/loans/admin/overdue}">逾期管理</a></li>
                        </ul>
                    </li>
                </ul>
                
                <!-- 搜索框 -->
                <form class="d-flex me-2" th:action="@{/books/search}" method="get">
                    <input class="form-control me-2" type="search" name="keyword" placeholder="搜索图书..." aria-label="Search">
                    <button class="btn btn-outline-light" type="submit">搜索</button>
                </form>
                
                <!-- 未登录显示登录注册按钮 -->
                <div class="navbar-nav" sec:authorize="!isAuthenticated()">
                    <a class="nav-link" th:href="@{/login}">登录</a>
                    <a class="nav-link" th:href="@{/register}">注册</a>
                </div>
                
                <!-- 已登录显示用户信息和退出按钮 -->
                <div class="navbar-nav" sec:authorize="isAuthenticated()">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button" data-bs-toggle="dropdown">
                            <i class="fas fa-user me-1"></i><span sec:authentication="name">用户</span>
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end">
                            <li><a class="dropdown-item" th:href="@{/profile}">个人资料</a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li>
                                <form th:action="@{/logout}" method="post" class="dropdown-item p-0">
                                    <button type="submit" class="btn btn-link text-decoration-none text-danger w-100 text-start ps-3">退出登录</button>
                                </form>
                            </li>
                        </ul>
                    </li>
                </div>
            </div>
        </div>
    </nav>

    <!-- 页面内容 -->
    <div th:fragment="content(content)" class="container mt-4 mb-5">
        <!-- 消息提示 -->
        <div th:if="${success}" class="alert alert-success alert-dismissible fade show" role="alert">
            <span th:text="${success}">操作成功</span>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        <div th:if="${error}" class="alert alert-danger alert-dismissible fade show" role="alert">
            <span th:text="${error}">操作失败</span>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        
        <!-- 实际内容 -->
        <div th:replace="${content}">页面内容</div>
    </div>

    <!-- 页脚 -->
    <footer th:fragment="footer" class="bg-dark text-white py-4 mt-5">
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <h5>图书馆管理系统</h5>
                    <p>提供图书借阅、归还和管理服务</p>
                </div>
                <div class="col-md-3">
                    <h5>快速链接</h5>
                    <ul class="list-unstyled">
                        <li><a class="text-white" th:href="@{/}">首页</a></li>
                        <li><a class="text-white" th:href="@{/books/list}">图书列表</a></li>
                        <li><a class="text-white" th:href="@{/books/search}">搜索图书</a></li>
                        <li><a class="text-white" th:href="@{/books/circuit-breaker}">熔断器测试</a></li>
                        <li><a class="text-white" th:href="@{/zipkin-test}">链路跟踪</a></li>
                    </ul>
                </div>
                <div class="col-md-3">
                    <h5>联系我们</h5>
                    <address>
                        <p><i class="fas fa-map-marker-alt me-2"></i>地址: 中国某省某市</p>
                        <p><i class="fas fa-phone me-2"></i>电话: (123) 456-7890</p>
                        <p><i class="fas fa-envelope me-2"></i>邮箱: info@library.com</p>
                    </address>
                </div>
            </div>
            <hr>
            <div class="text-center">
                <p>&copy; 2025 图书馆管理系统 版权所有</p>
            </div>
        </div>
    </footer>

    <!-- Bootstrap JS 和 Popper.js -->
    <div th:fragment="scripts">
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
        <!-- 自定义JS -->
        <script th:src="@{/js/main.js}"></script>
    </div>
</body>
</html>
